package kassiopeia.gui

import java.util.ArrayList;
import org.apache.log4j.Logger;
import java.io.File;
import java.util.List;
import java.util.zip.ZipFile;
import kassiopeia.commons.model.Coordinates;
import kassiopeia.commons.model.plugin.Output;
import kassiopeia.commons.model.plugin.Parameter;
import kassiopeia.commons.model.plugin.PluginDescriptor;
import kassiopeia.commons.model.xml.PluginDescriptorXmlSerializer;
import kassiopeia.gui.task.Task;

/**
 * This smart RepositoryService implementation searches for Kassiopeia plugins
 * in local Maven Repository.
 *
 * NOTE: You must restart container to reload!
 *
 */
class RepositoryMockService implements RepositoryService {

    private static final Logger log = Logger.getLogger(RepositoryMockService.class)

    static transactional = true

    def static mavenRepo = "c:\\Documents and Settings\\${System.getProperty('user.name')}\\.m2\\repository"
    def static groupId = 'kassiopeia'
    def static parentArtifactId = 'kassiopeia-plugins'
    def static parentVersion = '1.0-SNAPSHOT'
    def static pluginVersion = '1.0'

    List<PluginDescriptor> pluginDescriptors = new ArrayList<PluginDescriptor>()

    public RepositoryMockService() {
        loadDescriptorsFromMavenRepo()
    }

    @Override
    List<Coordinates> getAllCoordinates() {
        List<Coordinates> coords = new ArrayList<Coordinates>()
        pluginDescriptors.each {
            coords.add(it.coordinates)
        }
        return coords
    }

    @Override
    PluginDescriptor getDescriptor(Coordinates coordinates) {
        for(descriptor in pluginDescriptors) {
            if(descriptor.getCoordinates().equals(coordinates)) {
                return descriptor
            }
        }
    }

    @Override
    PluginDescriptor getDescriptor(String pluginId, String groupId, String version) {
        return getDescriptor(new Coordinates(pluginId, groupId, version))
    }

    private def loadDescriptorsFromMavenRepo() {
        def pom = new XmlParser().parse(
            new File("${mavenRepo}\\${groupId}\\${parentArtifactId}\\${parentVersion}\\${parentArtifactId}-${parentVersion}.pom"))

        pom.modules.module.each {
             String jarFileName = "${it.text()}-${pluginVersion}.jar"
             File pluginJar = new File("${mavenRepo}\\${groupId}\\${it.text()}\\${pluginVersion}\\${jarFileName}")
             ZipFile zipFile = new ZipFile(pluginJar)
             zipFile.entries().each { entry ->
                 if(entry.name.equals('META-INF/plugin.xml')) {
                     def pluginXml = zipFile.getInputStream(entry).getText()
                     try {
                         pluginDescriptors.add(PluginDescriptorXmlSerializer.fromXml(pluginXml))
                         log.info("Successfully loaded descriptor for '${jarFileName}'")
                     } catch(Exception exception) {
                         log.warn("Can't read plugin.xml from '${jarFileName}'",exception)
                     }
                 }
             }
        }
    }
}
